﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using DDF.Entity;
using ARR.UI.CommentsService;
using ARR.UI.RangeReviewsService;
using ARR.UI.CategoriesService;

namespace ARR.UI.Models
{
    public interface IGeneralCommentsModel : IModelBase
    {
        ObservableCollection<GeneralComment> GeneralComments { get; set; }

        ObservableCollection<Category> Categories { get; set; }

        ObservableCollection<RangeReview> RangeReviews { get; set; }

        void Save();

        void LoadGeneralComments();
    }

    public class GeneralCommentsModel : ModelBase, IGeneralCommentsModel
    {
        public GeneralCommentsModel()
        {
            GeneralComments = new ObservableCollection<GeneralComment>();
            RangeReviews = new ObservableCollection<RangeReview>();
            Categories = new ObservableCollection<Category>();

            LoadCategories();

            LoadGeneralComments();
            LoadRangeReviews();
        }

        public ObservableCollection<Category> Categories { get; set; }

        public ObservableCollection<GeneralComment> GeneralComments { get; set; }

        public ObservableCollection<RangeReview> RangeReviews { get; set; }

        public void Save()
        {
            var toSave = GeneralComments.Where(x => x.GetEntityState() != EntityState.Original);

            CommentsServiceClient service = new CommentsServiceClient();

            try
            {
                service.SaveGeneralComments(toSave.ToList());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void LoadGeneralComments()
        {
            CommentsServiceClient service = new CommentsServiceClient();

            try
            {
                GeneralComments = new ObservableCollection<GeneralComment>(service.GetAllGeneralComments());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }
        
        private void LoadRangeReviews()
        {
            RangeReviewsServiceClient service = new RangeReviewsServiceClient();

            try
            {
                RangeReviews = new ObservableCollection<RangeReview>(service.GetAll(WorkspaceData.Instance.Country.Id, true));
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }
        
        private void LoadCategories()
        {
            CategoriesServiceClient service = new CategoriesServiceClient();
            List<Category> categories = new List<Category>();

            try
            {
                categories = service.GetByLevels(2);
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

            categories.Add(new Category { Name = "TODAS", Code = "000000", Id = 0, Level = 2 });

            foreach (var category in categories.Where(x => x.Level == 2))
            {
                category.SubCategories = new List<Category>();
                category.SubCategories.AddRange(categories.Where(x => x.ParentId == category.Id));
            }
            Categories = new ObservableCollection<Category>(categories.Where(x => x.Level == 2).OrderBy(x => x.Name));




        }
    }
    
    public class GeneralCommentsModelMock : MockModelBase, IGeneralCommentsModel
    {
        public GeneralCommentsModelMock()
        {
            GeneralComments = new ObservableCollection<GeneralComment>();
            RangeReviews = new ObservableCollection<RangeReview>();
            Categories = new ObservableCollection<Category>();
        }

        public ObservableCollection<Category> Categories { get; set; }

        public ObservableCollection<GeneralComment> GeneralComments { get; set; }

        public ObservableCollection<RangeReview> RangeReviews { get; set; }

        public void Save()
        {

        }

        public void LoadGeneralComments()
        {
        }
    }
   
}
